package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.domain.Contract;
import com.ruoyi.system.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.mapper.ContractMapper;
import com.ruoyi.system.service.IContractService;

/**
 * 收房合同Service业务层处理
 * 
 * @author zhzaoxian
 * @date 2023-02-24
 */
@Service
public class ContractServiceImpl implements IContractService 
{
    @Autowired
    private ContractMapper contractMapper;

    /**
     * 查询收房合同
     * 
     * @param id 收房合同主键
     * @return 收房合同
     */
    @Override
    public Contract selectContractById(Long id)
    {
        return contractMapper.selectContractById(id);
    }

    /**
     * 查询收房合同列表
     * 
     * @param contract 收房合同
     * @return 收房合同
     */
    @Override
    public List<Contract> selectContractList(Contract contract)
    {
        return contractMapper.selectContractList(contract);
    }

    /**
     * 新增收房合同
     * 
     * @param contract 收房合同
     * @return 结果
     */
    @Transactional
    @Override
    public int insertContract(Contract contract)
    {
        contract.setCreateTime(DateUtils.getNowDate());
        int rows = contractMapper.insertContract(contract);
        insertSysUser(contract);
        return rows;
    }

    /**
     * 修改收房合同
     * 
     * @param contract 收房合同
     * @return 结果
     */
    @Transactional
    @Override
    public int updateContract(Contract contract)
    {
        contract.setUpdateTime(DateUtils.getNowDate());
        contractMapper.deleteSysUserByUserId(contract.getId());
        insertSysUser(contract);
        return contractMapper.updateContract(contract);
    }

    /**
     * 批量删除收房合同
     * 
     * @param ids 需要删除的收房合同主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteContractByIds(Long[] ids)
    {
        return contractMapper.deleteContractByIds(ids);
    }

    /**
     * 删除收房合同信息
     * 
     * @param id 收房合同主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteContractById(Long id)
    {
        contractMapper.deleteSysUserByUserId(id);
        return contractMapper.deleteContractById(id);
    }

    /**
     * 新增用户信息信息
     * 
     * @param contract 收房合同对象
     */
    public void insertSysUser(Contract contract)
    {
        List<User> userList = contract.getSysUserList();
        Long id = contract.getId();
        if (StringUtils.isNotNull(userList))
        {
            List<User> list = new ArrayList<User>();
            for (User user : userList)
            {
                user.setUserId(id);
                list.add(user);
            }
            if (list.size() > 0)
            {
                contractMapper.batchSysUser(list);
            }
        }
    }
}
